﻿using System;
using System.IO;

namespace CatUtils.Utils.Currency.IoUtils
{
    /// <summary>
    /// Steam帮助工具类
    /// </summary>
    public class StreamHelper
    {
        /// <summary>
        /// 获取文件大小 返回的是KB
        /// </summary>
        /// <param name="FileStream"></param>
        /// <returns></returns>
        public static double GetStreamSizeForKB(Stream FileStream)
        {
            return (GetStreamSizeForByte(FileStream) / 1024.00);
        }

        /// <summary>
        /// 获取文件大小 返回的是Byte
        /// </summary>
        /// <param name="FileStream"></param>
        /// <returns></returns>
        public static double GetStreamSizeForByte(Stream FileStream)
        {
            if (FileStream == null)
                return 0;
            long FactSize = FileStream.Length;

            return FactSize;
        }

        /// <summary>
        /// 文件转换为Stream
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static Stream FileToStream(string FilePath)
        {
            Stream stream = null;
            // 打开文件

            using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                // 读取文件的 byte[]

                byte[] bytes = new byte[fileStream.Length];

                fileStream.Read(bytes, 0, bytes.Length);

                fileStream.Close();

                // 把 byte[] 转换成 Stream

                stream = new MemoryStream(bytes);
            }

            return stream;
        }

        /// <summary>
        /// Stream转换为文件
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="LocalFilestream"></param>
        /// <returns></returns>
        public static string StreamToFile(string FilePath, Stream LocalFilestream)
        {
            try
            {
                // 把 Stream 转换成 byte[]

                byte[] bytes = new byte[LocalFilestream.Length];
                using (LocalFilestream)
                {
                    LocalFilestream.Read(bytes, 0, bytes.Length);

                    // 设置当前流的位置为流的开始
                    LocalFilestream.Seek(0, SeekOrigin.Begin);

                    using (FileStream fs = new FileStream(FilePath, FileMode.Create))
                    {    // 把 byte[] 写入文件
                        using (BinaryWriter bw = new BinaryWriter(fs))
                        {
                            bw.Write(bytes);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                PrintLog.E(ex);
            }
            return FilePath;
        }

        /// <summary>
        ///  将 Stream 转成 byte[]
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] StreamToBytes(Stream stream)

        {
            byte[] bytes = null;
            try
            {
                bytes = new byte[stream.Length];

                stream.Read(bytes, 0, bytes.Length);

                // 设置当前流的位置为流的开始

                stream.Seek(0, SeekOrigin.Begin);
            }
            catch (Exception ex)
            {
                PrintLog.E(ex);
            }

            return bytes;
        }

        /// <summary>
        /// 将 byte[] 转成 Stream
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static Stream BytesToStream(byte[] bytes)

        {
            Stream stream = null;
            try
            {
                stream = new MemoryStream(bytes);
            }
            catch (Exception ex)
            {
                PrintLog.E(ex);
            }

            return stream;
        }
    }
}